package com.xiaoshu.controller;

import com.xiaoshu.model.ResultCode;
import com.xiaoshu.model.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 功能说明： 负责文件上传操作，查询文件操作，删除文件等操作；
 *
 * @ com.xiaoshu.controller
 * <p>
 * Original @Author: deane.jia-贾亮亮,@2020/10/28@10:13
 * <p>
 * Copyright (C)2012-@2020 深圳小树盛凯科技 All rights reserved.
 */
@RestController
@RequestMapping(value = "/produce/file")
@Slf4j
@Api(value = "文件服务")
public class FileServiceController {

    /**
     * 上传文件服务，负责文件上传操作
     *
     * @param file
     * @return
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "文件上传")
    public ResultVo<?> uploadFileService(@ApiParam(value = "file") @RequestPart("file") MultipartFile file) {
        log.info("start upload the file , file name is :{}", file.getOriginalFilename());
        String fileName = file.getOriginalFilename();
        String fileType = fileName.substring(fileName.indexOf("."));
        File newFile = new File("D:/temp/" + System.currentTimeMillis() + fileType);
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 返回
        return new ResultVo<>(ResultCode.SUCCESS, newFile.getName());
    }
}
